<?php


namespace app\api\controller\v42;


use app\api\controller\v13\Auth2;
use app\common\cachemodel\Fortune;
use app\common\cachemodel\ZlFortuneUp;
use app\common\lib\exception\ApiException;
use app\common\lib\Util;
use app\common\model\Recode;
use app\common\model\Recode as RecodeModel;

/**
 * V2.3.0 商城
 * 本命报告
 * Class NatalReport
 * @package app\api\controller\v20
 */
class NatalReport extends Auth2
{
    /**
     * 获取不同运势的信息 v2.4.1厄运消除  去除之前版本厄运消除数据结构
     * @throws ApiException
     * @throws \Exception
     */
    public function multipleFortune()
    {
        $type = input('type/d', 0);
        $in_sign = input('in_sign/d', 1);
        $fortuneModel = new Fortune();

        if ($this->user && $this->user['rid'])
        {
            $map = ['uid' => $this->user['id'], 'isself' => 1];
            $recode_info = (new RecodeModel())->getSelfRecode($map);

            if (!$recode_info || !$recode_info['sun'])
            {
                return show(406, '档案缺失请添加档案');
            }
            $in_sign = explode('-', $recode_info['sun'])[0];
        } elseif (!in_array($in_sign, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], true))
        {
            return show(408, '请选择星座');
        }

        if (!$this->user && $type === 5)
        {
            throw new ApiException('您的账户登录过期，请重新登录。', 200, config('code.denied'));
        }

        switch ($type)
        {
            case 1:
                if ($this->user && $this->user['rid'])
                {
                    $ver = Util::CheckAppVersion('2.9.4') === '小' ? 1 : 3;
                    $data = $fortuneModel->todayInfoV2($this->user, [], $ver);
                    // 获取运势提升状态 1已提升 0未提升
                    $isFortuneUp = (new ZlFortuneUp())->getFortuneUpStatus((int)$this->user['rid'], (int)(date('Ymd')));
                    if ($isFortuneUp)
                    {
                        $data['today']['fortune_up_status'] = 2;
                    } else
                    {
                        $data['today']['fortune_up_status'] = 0;
                        foreach ($data['today']['list'] as $item)
                        {
                            if (in_array($item['color'], ['#ffed17', '#ffa536', '#7775ff'], true))
                            {
                                $data['today']['fortune_up_status'] = 1;
                                break;
                            }
                        }
                    }
                } else
                {
                    $data = $fortuneModel->todayArticle($in_sign);
                }
                break;
            case 2:
                $data = $fortuneModel->weekInfo2($in_sign);
                break;
            case 3:
                if ($this->user && $this->user['rid'])
                {
                    $data = $fortuneModel->monthInfo($this->user, $this->headers['version']);
                } else
                {
                    $data = $fortuneModel->monthArticle($in_sign);
                }
                break;
            case 4:
                if ($this->user && $this->user['rid'])
                {
                    $data = $fortuneModel->yearInfo($this->user, $this->headers['version']);
                } else
                {
                    $data = $fortuneModel->yearArticle($in_sign);
                }
                break;
            case 5:
                $data = $fortuneModel->reportInfo($this->user);
                break;
            default:
                $data = [];
                break;
        }

        if ($data)
        {
            return show(config('code.success'), '获取成功', $data);
        }
        return show(config('code.error'), '未知的运势');
    }
}
